using System;
using System.Collections.Generic;

using AIFSContentManagementSystem.Criteria;

namespace AIFSContentManagementSystem.Articles
{
    public class Article : ModelBase, IParsable
	{       
        int? articleId;

        public int? ArticleId
        {
            get { return articleId; }
            set { articleId = value; }
        }

        ArticleType articleType;

		public ArticleType ArticleType {
			get 
            {
				return this.articleType;
			}
			set 
            {
				articleType = value;
			}
		}

        List<Criterion> _Criteria;

        public virtual List<Criterion> Criteria
        {
            get { return _Criteria; }
            set { _Criteria = value; }
        }

		DateTime? effectiveFrom;

        public DateTime? EffectiveFrom
        {
            get { return effectiveFrom; }
            set { effectiveFrom = value; }
        }

        DateTime? effectiveThru;

        public DateTime? EffectiveThru
        {
            get { return effectiveThru; }
            set { effectiveThru = value; }
        }

        
        bool? active;

        public bool? Active
        {
            get { return active; }
            set { active = value; }
        }

        string program;

        public string Program
        {
            get { return program; }
            set { program = value; }
        }

		public Article ()
		{
			articleType = ArticleType.UNSET;
			fragments = new List<Fragment>();
			placeHolders = new List<PlaceHolder>();
            _Criteria = new List<Criterion>();
		}
                
        #region IParsable Members

        List<Fragment> fragments;
        List<PlaceHolder> placeHolders;
        
        public virtual List<Fragment> Fragments
        {
            get { return fragments; }
            set
            {
                fragments = value;
            }
        }

        public List<PlaceHolder> PlaceHolders
        {
            get { return this.placeHolders; }
            set { this.placeHolders = value; }
        }
        #endregion

        /// <summary>
        /// Method tests whether the criteria list provided "fits" the criteria associated with this article.
        /// </summary>        
        /// <returns><c>true</c> if meets one of the criterion or <c>false</c> if does not meet any of the criteria</returns>
        public bool IsMatch(List<Criterion> CriteriaToMatch)
        {
            bool ismatch = false;
            //This article does not have any criteria so it is a "global" article
            if(this.Criteria == null || this.Criteria.Count == 0)
                return true;

            foreach (Criterion criterion in this.Criteria)
            {
                ismatch = criterion.IsMatch(CriteriaToMatch);
                if (ismatch)
                    break;
            }

            return ismatch;
        }
    }
}
